package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.offcn.entity.User;
import com.offcn.service.Impl.UserServiceImpl;
import com.offcn.service.UserService;
import com.offcn.utils.PageTool;
import com.offcn.utils.ResultBean;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.DbUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    ResultBean bean = null;

    //    修改用户
    protected void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        BeanUtils.populate(user, map);
        UserService service = new UserServiceImpl();
        int i = service.updateUser(user);
        if (i >= 0) {
            bean = new ResultBean(200, "修改成功", i);
        } else {
            bean = new ResultBean(500, "修改失败", null);
        }
        String s = JSON.toJSONString(bean);
        response.getWriter().write(s);
    }

    //    查询全部用户
    protected void findAllUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String search = request.getParameter("search");
        String pageSize = request.getParameter("pageSize");
        String currentPage = request.getParameter("currentPage");
        UserService service = new UserServiceImpl();
        int totalCount = service.countUser(search);
        PageTool page = new PageTool(totalCount, currentPage, Integer.parseInt(pageSize));
        List<User> list = service.findAllUser(page, search);
        for (User u : list) {
            System.out.println(u.getName());
        }
        page.setTotalCount(totalCount);
        if (list != null) {
            page.setList(list);
            bean = new ResultBean(200, "查询成功", page);
        } else {
            bean = new ResultBean(500, "查询失败", null);
        }
        String s = JSON.toJSONString(bean);
        response.getWriter().write(s);
    }

    protected void getTotalCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String search = request.getParameter("search");
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        UserService service = new UserServiceImpl();
        int totalCount = service.countUser(search);
        if (totalCount >= 0)
            bean = new ResultBean(200, "查询成功", totalCount);
        else
            bean = new ResultBean(500, "查询失败", null);
        String s = JSON.toJSONString(bean);
        response.getWriter().write(s);
    }

    //    添加用户
    protected void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        BeanUtils.populate(user, map);
        UserService service = new UserServiceImpl();
        int i = service.addUser(user);
        if (i >= 0) {
            bean = new ResultBean(200, "添加成功", i);
        } else {
            bean = new ResultBean(500, "添加失败", null);
        }
        String s = JSON.toJSONString(bean);
        response.getWriter().write(s);
    }

    //    用户登录
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        HttpSession session = request.getSession();
        String code = (String) session.getAttribute("verCode");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String vercode = request.getParameter("imageCode");
        if (!vercode.equals(code)) {
            bean = new ResultBean(500, "验证码有误", null);
            response.getWriter().write(JSON.toJSONString(bean));
        } else {
            UserService service = new UserServiceImpl();
            User user = service.login(new User(username, password));
            if (user != null) {
                request.setAttribute("user", user);
                bean = new ResultBean(200, "登录成功", user);
            } else {
                bean = new ResultBean(500, "账号或密码有误", "账号或密码有误");
            }
            response.getWriter().write(JSON.toJSONString(bean));
        }
    }

    //    删除用户
    protected void delAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        String[] delarrs = request.getParameterValues("delarr");
        String s = Arrays.toString(delarrs);
        String arr = "(" + s.substring(1, s.length() - 1) + ")";
        UserService service = new UserServiceImpl();
        int i = service.delAll(arr);
        if (i > 0) {
            bean = new ResultBean(200, "删除成功", null);
        } else if (i == 0) {
            bean = new ResultBean(500, "删除失败，未查询到删除对象", null);
        } else {
            bean = new ResultBean(500, "删除失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    /***************************************用户页面业务*******************************************************/

//手机号验证
    protected void findByPhone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String phone = request.getParameter("phone");
        UserService service = new UserServiceImpl();
        User user = service.findByPhone(phone);
        if (user != null) {
            bean = new ResultBean(200, "查询成功", user);
        } else {
            bean = new ResultBean(500, "查询失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    //    用户注册
    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        String verCode = request.getSession().getAttribute("verCode").toString();
        System.out.println("接收到" + name + "###" + phone + "###" + password + "###" + code + "###" + verCode);
        if (!code.equals(verCode)) {
            bean = new ResultBean(500, "验证码有误", null);
            response.getWriter().write(JSON.toJSONString(bean));
            return;
        } else {
            UserService service = new UserServiceImpl();
            int i = service.regist(name, phone, password);
            if (i > 0) {
                User user = service.findByPhone(phone);
                bean = new ResultBean(200, "注册成功", user);
            } else {
                bean = new ResultBean(500, "注册失败", null);
            }
            response.getWriter().write(JSON.toJSONString(bean));
        }
    }

    //    手机号登录
    protected void loginByPhone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        UserService service = new UserServiceImpl();
        User byPhone = service.findByPhone(phone);
        if (byPhone == null || !byPhone.getPassword().equals(password)) {
            System.out.println("登陆失败");
            bean = new ResultBean(500, "登录失败", null);
        } else {
            System.out.println("登录成功");
            bean = new ResultBean(200, "登录成功", byPhone);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }
}
